import storage from 'good-storage'

const SEARCH_KEY = '_search_'

function inserArray(arr, val, compare, maxLen) {
  const index =  arr.findIndex(compare)
  if(!index) return
  if(index>0) arr.splice(index,1)
  arr.unshift(val)
  if(maxLen&&arr.length>maxLen) arr.pop()
}

export function saveSearch(query){
  let searches = storage.get(SEARCH_KEY,[])
  //插入列表 删除重复数据
  inserArray(searches,query,(item) => {
    return item == query
  },15)
  storage.set(SEARCH_KEY,searches)
  return searches
}

export function loadSearch() {
  return storage.get(SEARCH_KEY, [])
}
